import { hyRequest, hyReqInstance } from "../../service/index";

Page({
  data: {
    allCities: {},
    houselist: [],
    page: 1,
  },
  async onLoad() {
    // 4. await/async 封装成函数
    this.getCityData();
    this.getHouseListData();

    // 5. 使用自定义请求类发送请求
    hyReqInstance
      .get({
        url: "/city/all",
      })
      .then((res) => {
        console.log(res, "🚀🚀🚀🚀");
      });

    // 3. await/async (只有前面的网络请求发送完成后,才会执行下一个方法)
    /*  const cityRes = await hyRequest({url: 'http://123.207.32.32:1888/api/city/all'})
    this.setData({
      allCities: cityRes.data
    })

    const houseRes2 = await hyRequest({url: 'http://123.207.32.32:1888/api/home/houselist', data: {page: this.data.page++}})
    this.setData({
      houselist: houseRes2.data
    }) */

    // 方式二(封装函数)
    /* hyRequest({
      url: 'http://123.207.32.32:1888/api/city/all',
    }).then(res => {
      console.log(res)
      this.setData({
        allCities: res.data
      })
    })

    hyRequest({
      url: 'http://123.207.32.32:1888/api/home/houselist',
      data: {
        page: this.data.page++
      }
    }).then(res => {
      this.setData({
        houselist: res.data
      })
    }) */

    // 方式一(未封装)
    /* wx.request({
      url: 'http://123.207.32.32:1888/api/city/all',
      success: (res) => {
        this.setData({
          allCities: res.data.data
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })

    wx.request({
      url: 'http://123.207.32.32:1888/api/home/houselist',
      data: {
        page: this.data.page++
      },
      success: res => {
        this.setData({
          houselist: res.data.data
        })
      },
      fail: err => {
        console.log(err)
      }
    }) */
  },
  async getCityData() {
    const cityRes = await hyRequest({
      url: "http://123.207.32.32:1888/api/city/all",
    });
    this.setData({
      allCities: cityRes.data,
    });
  },
  async getHouseListData() {
    const houseRes2 = await hyRequest({
      url: "http://123.207.32.32:1888/api/home/houselist",
      data: { page: this.data.page++ },
    });
    this.setData({
      houselist: [...houseRes2.data, ...this.data.houselist],
    });
  },
  onReachBottom() {
    this.getHouseListData();
  },
});
